/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;
import Class.Album;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import Class.process_XML;
/**
 *
 * @author thu
 */
public class NewAlbum extends javax.swing.JDialog {

    /**
     * Creates new form NewAlbum
     */
    private static Album alb;
    private static ArrayList<Album>arrAlbums;
    private static final String fileAlbum = "/Lib/Album.xml";
    private boolean isClose; 
    public boolean getIsClose()
    {
        return this.isClose;
    }
    public NewAlbum(java.awt.Frame parent, boolean modal,Album _alb) {
        super(parent, modal);
        initComponents();
        this.alb=_alb;
        this.isClose=false;
    }

    private NewAlbum(JFrame jFrame, boolean b) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        dtpDate = new org.jdesktop.swingx.JXDatePicker();
        jLabel2 = new javax.swing.JLabel();
        jXButton1 = new org.jdesktop.swingx.JXButton();
        txtTitle = new javax.swing.JTextField();
        txtTitle1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Tên Allbum");

        dtpDate.setDate(new Date());

        jLabel2.setText("Ngày");

        jXButton1.setText("Ngày Tự Động");
        jXButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jXButton1MouseClicked(evt);
            }
        });

        txtTitle1.setText("Tiêu đề (Tùy Chọn)");

        jLabel5.setText("Mô Tả (Tùy Chọn)");

        txtDescription.setColumns(20);
        txtDescription.setRows(5);
        jScrollPane1.setViewportView(txtDescription);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Ok.png"))); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Cancle.png"))); // NOI18N
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(txtTitle1)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtTitle, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(dtpDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jXButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(18, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(dtpDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTitle1)
                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        this.setLocationRelativeTo(null);
        String pathAlbum=getClass().getResource(fileAlbum).getPath();
        arrAlbums=process_XML.getAlbums(pathAlbum);
            
        if(!alb.getName().equalsIgnoreCase(""))
        {
            txtName.setText(alb.getName());
            String dat=alb.getDate();
            //Date d=Date.valueOf(alb.getDate());
            try
            {
                SimpleDateFormat fd = new SimpleDateFormat("MM/dd/yyyy");
                Date d=fd.parse(dat);
                //String NS=fd.format(dat);
                System.out.print("");
                
                dtpDate.setDate(d);
                //dtpDate.setDate(Date.valueOf(NS));
            }
            catch(Exception ex)
            {
                System.out.print("");
            }
            txtTitle.setText(alb.getTitle());
            txtDescription.setText(alb.getDescription());
        }
        
    }//GEN-LAST:event_formWindowOpened

    private void jXButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jXButton1MouseClicked
        // TODO add your handling code here:
        java.util.Date d=new java.util.Date();
        dtpDate.setDate(d);
    }//GEN-LAST:event_jXButton1MouseClicked

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        // TODO add your handling code here:
        this.setVisible(false);
        this.isClose=true;
    }//GEN-LAST:event_jButton3MouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        // TODO add your handling code here:
        String strLoi="";
        if(txtName.getText().equalsIgnoreCase(""))
            strLoi+="Nhập thông tin tên Album\n";
        if(txtTitle.getText().equalsIgnoreCase(""))
            strLoi+="Nhập thông tin tiêu đề Album\n";
        if(txtDescription.getText().equalsIgnoreCase(""))
            strLoi+="Nhập thông tin mô tả Album";
        if(strLoi.equalsIgnoreCase(""))
        {
            if(alb.getName().equalsIgnoreCase(""))
            {
                int n=arrAlbums.size();
                boolean flag=true;
                for(int i=0;i<n;i++)
                {
                    if(txtName.getText().equalsIgnoreCase(arrAlbums.get(i).getName()))
                    {
                        flag=false;
                        break;
                        
                    }
                }
                if(flag)
                {
                    String pathAlbum=getClass().getResource(fileAlbum).getPath();
                    alb.setName(txtName.getText());
                    Date d=dtpDate.getDate();
                    SimpleDateFormat fd = new SimpleDateFormat("MM/dd/yyyy");
                    String NS=fd.format(d);
                    alb.setDate(NS);
                    alb.setDescription(txtDescription.getText());
                    alb.setTitle(txtTitle.getText());
                    process_XML.AddAlbumXML(pathAlbum,alb);
                    this.isClose=false;
                    this.setVisible(false);
                }
                else
                    JOptionPane.showMessageDialog(null, "Tên Album đã tồn tại");
            }
            else
            {
                int n=arrAlbums.size();
                boolean flag=true;
                for(int i=0;i<n;i++)
                {
                    if(txtName.getText().equalsIgnoreCase(arrAlbums.get(i).getName()) && !txtName.getText().equalsIgnoreCase(alb.getName()))
                    {
                        flag=false;
                        break;
                        
                    }
                }
                if(flag)
                {
                    String pathAlbum=getClass().getResource(fileAlbum).getPath();
                    String oldName=alb.getName();
                    alb.setName(txtName.getText());
                    Date d=dtpDate.getDate();
                    SimpleDateFormat fd = new SimpleDateFormat("MM/dd/yyyy");
                    String NS=fd.format(d);
                    alb.setDate(NS);
                    alb.setDescription(txtDescription.getText());
                    alb.setTitle(txtTitle.getText());
                    process_XML.EditInfoAlbumXML(pathAlbum,oldName, alb);
                    this.isClose=false;
                    this.setVisible(false);
                }
                else
                    JOptionPane.showMessageDialog(null, "Tên Album đã tồn tại");
            }
        }
        else
            JOptionPane.showMessageDialog(null, strLoi);
    }//GEN-LAST:event_jButton2MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewAlbum.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewAlbum.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewAlbum.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewAlbum.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewAlbum dialog = new NewAlbum(new javax.swing.JFrame(), true,alb);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXDatePicker dtpDate;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXButton jXButton1;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtTitle;
    private javax.swing.JLabel txtTitle1;
    // End of variables declaration//GEN-END:variables
}
